package com.wehaicao.wehaicaoshardingjdbc.init;


import com.alibaba.druid.pool.DruidDataSource;
import com.wehaicao.wehaicaoshardingjdbc.config.Profile;
import com.wehaicao.wehaicaoshardingjdbc.config.ShardingJdbcProperties;
import com.wehaicao.wehaicaoshardingjdbc.constant.ShardingJdbcConstant;
import io.shardingjdbc.core.api.MasterSlaveDataSourceFactory;
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.*;

@Slf4j
@ConditionalOnBean(ShardingJdbcProperties.class)
@Configuration
public class DruidDataSourceInit {


    @Autowired(required = false)
    private ShardingJdbcProperties shardingJdbcProperties ;

    @Bean
    @ConditionalOnBean(ShardingJdbcProperties.class)
    public DataSource init(){
        DataSource dataSource =null ;
        // 配置真实数据源
        Map<String, DataSource> dataSourceMap = new HashMap<>();
        //TODO 初始化主库信息
        DruidDataSource masterDataSource = new DruidDataSource();
        masterDataSource.setUrl(shardingJdbcProperties.getMaster().getUrl());
        masterDataSource.setUsername(shardingJdbcProperties.getMaster().getUsername());
        masterDataSource.setPassword(shardingJdbcProperties.getMaster().getPassword());
        DataSourceDirector.buildDataSource(masterDataSource,shardingJdbcProperties);
        dataSourceMap.put(ShardingJdbcConstant.MASTER_DB_NAME, masterDataSource);

        // 配置读写分离规则
        MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration();
        //主从库名称
        masterSlaveRuleConfig.setName(StringUtils.isBlank(shardingJdbcProperties.getClusterName())?ShardingJdbcConstant.DEFAULT_CLUSTER_NAME:shardingJdbcProperties.getClusterName());
        masterSlaveRuleConfig.setMasterDataSourceName(ShardingJdbcConstant.MASTER_DB_NAME);

        int size = shardingJdbcProperties.getSlaves().size();
        if(size>0){
            List slaveDbNames = new ArrayList(size);
            for (int i = 0; i < size; i++) {
                // 配置从库
                DruidDataSource slaveDataSource = new DruidDataSource();
                slaveDataSource.setUrl(shardingJdbcProperties.getSlaves().get(i).getUrl());
                slaveDataSource.setUsername(shardingJdbcProperties.getSlaves().get(i).getUsername());
                slaveDataSource.setPassword(shardingJdbcProperties.getSlaves().get(i).getPassword());
                DataSourceDirector.buildDataSource(slaveDataSource,shardingJdbcProperties);
                dataSourceMap.put("ds_slave"+i, slaveDataSource);
                slaveDbNames.add("ds_slave"+i);
            }
            masterSlaveRuleConfig.setSlaveDataSourceNames(slaveDbNames);
        }

        try {
            dataSource = MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveRuleConfig, new HashMap<>());
        } catch (SQLException e) {
            log.error("init masterSlave exception :{}",e);
        }
        return dataSource;
    }
}
